package org.zheng.shop.entity;

import java.time.LocalDateTime;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

/**
 * 咨询会话实体类
 * 用于管理用户与客服之间的咨询会话
 */
@Entity
@Table(name = "consultation_sessions")
public class ConsultationSession {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    /**
     * 用户ID
     */
    @Column(name = "user_id", nullable = false)
    private Integer userId;
    
    /**
     * 会话状态：ACTIVE(活跃), CLOSED(已关闭), WAITING(等待中)
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false)
    private SessionStatus status = SessionStatus.WAITING;
    
    /**
     * 会话创建时间
     */
    @Column(name = "created_at", nullable = false)
    private LocalDateTime createdAt;
    
    /**
     * 会话更新时间
     */
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;
    
    /**
     * 最后一条消息时间
     */
    @Column(name = "last_message_at")
    private LocalDateTime lastMessageAt;
    
    /**
     * 处理该会话的管理员ID
     */
    @Column(name = "admin_id")
    private Integer adminId;
    
    /**
     * 会话标题/主题
     */
    @Column(name = "title", length = 200)
    private String title;
    
    /**
     * 用户信息（冗余存储，便于查询）
     */
    @Column(name = "user_name", length = 100)
    private String userName;
    
    /**
     * 未读消息数量（管理员视角）
     */
    @Column(name = "unread_count", nullable = false)
    private Integer unreadCount = 0;
    
    // 会话状态枚举
    public enum SessionStatus {
        WAITING,    // 等待客服接入
        ACTIVE,     // 活跃会话
        CLOSED      // 已关闭
    }
    
    // 构造函数
    public ConsultationSession() {
        this.createdAt = LocalDateTime.now();
        this.updatedAt = LocalDateTime.now();
    }
    
    public ConsultationSession(Integer userId, String userName, String title) {
        this();
        this.userId = userId;
        this.userName = userName;
        this.title = title;
    }
    
    // Getters and Setters
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }
    
    public SessionStatus getStatus() {
        return status;
    }
    
    public void setStatus(SessionStatus status) {
        this.status = status;
        this.updatedAt = LocalDateTime.now();
    }
    
    public LocalDateTime getCreatedAt() {
        return createdAt;
    }
    
    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }
    
    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }
    
    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }
    
    public LocalDateTime getLastMessageAt() {
        return lastMessageAt;
    }
    
    public void setLastMessageAt(LocalDateTime lastMessageAt) {
        this.lastMessageAt = lastMessageAt;
        this.updatedAt = LocalDateTime.now();
    }
    
    public Integer getAdminId() {
        return adminId;
    }

    public void setAdminId(Integer adminId) {
        this.adminId = adminId;
    }
    
    public String getTitle() {
        return title;
    }
    
    public void setTitle(String title) {
        this.title = title;
    }
    
    public String getUserName() {
        return userName;
    }
    
    public void setUserName(String userName) {
        this.userName = userName;
    }
    
    public Integer getUnreadCount() {
        return unreadCount;
    }
    
    public void setUnreadCount(Integer unreadCount) {
        this.unreadCount = unreadCount;
    }
    
    /**
     * 增加未读消息数量
     */
    public void incrementUnreadCount() {
        this.unreadCount++;
    }
    
    /**
     * 清零未读消息数量
     */
    public void clearUnreadCount() {
        this.unreadCount = 0;
    }
}
